<?php
class ModelSetting extends CI_Model {

	/**
	 * Function __construct().
	 * used for Initialize ModelSetting Models
	 * @autor Biplab Biswas
	 * @date
	 */
	
	function __construct()
    {
        parent::__construct();
    }

    /**
	 * Function getGlobalData().
	 * used for get global data
	 * @autor Biplab Biswas
	 * @date
	 */
    
	function getGlobalData()
	{
		$sql="SELECT * FROM ".GLOBALCONFIG." ";
		$recordSet = $this->db->query($sql);

		$rs = false;
		if ($recordSet->num_rows() > 0)
        {
           	$rs = array();
			$isEscapeArr = array();
			foreach ($recordSet->result_array() as $row)
				{
					foreach($row as $key=>$val){
						if(!in_array($key,$isEscapeArr)){
							$recordSet->fields[$key] = outputEscapeString($val);
						}
					}
					$rs[$recordSet->fields['field_key']] = $recordSet->fields;
				}
        }
		return $rs;
	}
	
	/**
	 * Function updateGlobalSetting($data).
	 * used update global data
	 * @param $data 
	 * @param $data array passing post value
	 */
	
	function updateGlobalSetting($data)
	{
		/***************** Site Information *****************/
		if($data['mode'] == 'site_info'){
		
		$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['global_site_name']."' WHERE field_key='global_site_name' ";
		$recordSet = $this->db->query($sql);
		$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['global_meta_title']."' WHERE field_key='global_meta_title' ";
		$recordSet = $this->db->query($sql);
		$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['global_meta_keywords']."' WHERE field_key='global_meta_keywords' ";
		$recordSet = $this->db->query($sql);
		$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['global_meta_description']."' WHERE field_key='global_meta_description' ";
		$recordSet = $this->db->query($sql);
		
		$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['global_site_url']."' WHERE field_key='global_site_url' ";
		$recordSet = $this->db->query($sql);
		/*$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['logo']."' WHERE field_key='global_site_logo' ";
		$recordSet = $this->db->query($sql);*/
		/***************** Social Site Link *****************/
		$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['global_facebook_url']."' WHERE field_key='global_facebook_url' ";
		$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['fbappid']."' WHERE field_key='fbappid' ";
		$recordSet = $this->db->query($sql);
		$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['fbappsecret']."' WHERE field_key='fbappsecret' ";
		$recordSet = $this->db->query($sql);
		$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['global_twitter_url']."' WHERE field_key='global_twitter_url' ";
		$recordSet = $this->db->query($sql);
		$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['global_google_plus_url']."' WHERE field_key='global_google_plus_url' ";
		$recordSet = $this->db->query($sql);
		$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['global_pinterest_url']."' WHERE field_key='global_pinterest_url' ";
		$recordSet = $this->db->query($sql);
		$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['global_youtube_url']."' WHERE field_key='global_youtube_url' ";
		$recordSet = $this->db->query($sql);
		
		/*****************Contact information *****************/
		
		
		$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['global_telephone_number']."' WHERE field_key='global_telephone_number' ";
		$recordSet = $this->db->query($sql);
		
		$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['global_fax']."' WHERE field_key='global_fax' ";
		$recordSet = $this->db->query($sql);
		
		$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['global_mobile_number']."' WHERE field_key='global_mobile_number' ";
		$recordSet = $this->db->query($sql);
		
		$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['global_store_timings']."' WHERE field_key='global_store_timings' ";
		$recordSet = $this->db->query($sql);

$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['global_address']."' WHERE field_key='global_address' ";
		$recordSet = $this->db->query($sql);
		
		}
		/***************** Currency Conversion Rate Information *****************/
		
		if($data['mode'] == 'currency_conversion'){
		    
		$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['global_currency_conversion_rate']."' WHERE field_key='global_currency_conversion_rate' ";
		$recordSet = $this->db->query($sql);
		
		}
		/***************** Authorize.Net Information *****************/
		
		if($data['mode'] == 'payment_info'){
		  
		$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['global_auth_net_tran_key']."' WHERE field_key='global_auth_net_tran_key' ";
		$recordSet = $this->db->query($sql);
		$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['global_auth_net_login_id']."' WHERE field_key='global_auth_net_login_id' ";
		$recordSet = $this->db->query($sql);
		$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['global_auth_net_url']."' WHERE field_key='global_auth_net_url' ";
		$recordSet = $this->db->query($sql);
		$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['global_auth_net_mode']."' WHERE field_key='global_auth_net_mode' ";
		$recordSet = $this->db->query($sql);
		
		}
		/***************** Email Information *****************/
	    if($data['mode'] == 'email_info'){
		
		$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['global_web_admin_name']."' WHERE field_key='global_web_admin_name' ";
		$recordSet = $this->db->query($sql);
		$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['global_webadmin_email']."' WHERE field_key='global_webadmin_email' ";
		$recordSet = $this->db->query($sql);
		$sql = "UPDATE ".GLOBALCONFIG." SET field_value = '".$data['global_email_signature']."' WHERE field_key='global_email_signature' ";
		$recordSet = $this->db->query($sql);
	    }
		return true;
	}
	
	/**
	 * Function getListEmailTemplate(&$config,&$start,&$param).
	 * used for get template list 
	 * @param &$config array for pagination 
	 * @param &$start int for start index of pagination
	 * @param &$param array for search value
	 * @autor Santu Saha
	 * @date
	 */
	
	function getListEmailTemplate(&$config,&$start,&$param)
	{	
		// GET DATA FROM GET/POST  OR   SESSION ====================
		$Count = 0;
		$page = $this->uri->segment(3,0); // page
		$isSession = $this->uri->segment(4,0); // read data from SESSION or POST     (1 == POST , 0 = SESSION )
	
		$start = 0;
	
		$sortType 		= $param['sortType'];
		$sortField 		= $param['sortField'];
		$searchField 	= $param['searchField'];
		$searchText 	= $param['searchText'];
		$searchDisplay 	= $param['searchDisplay'];
	
		if($isSession == 0)
		{
			$sortType    	= $this->nsession->get_param('ADMIN_EMAIL_TEMPLATE','sortType','ASC');
			$sortField   	= $this->nsession->get_param('ADMIN_EMAIL_TEMPLATE','sortField','template_name');
			$searchField 	= $this->nsession->get_param('ADMIN_EMAIL_TEMPLATE','searchField','');
			$searchText  	= $this->nsession->get_param('ADMIN_EMAIL_TEMPLATE','searchText','');
			$searchDisplay  = $this->nsession->get_param('ADMIN_EMAIL_TEMPLATE','searchDisplay',20);
		}
	
		//========= SET SESSION DATA FOR SEARCH / PAGE / SORT Condition etc =====================
		$sessionDataArray = array();
		$sessionDataArray['sortType'] 		= $sortType;
		$sessionDataArray['sortField'] 		= $sortField;
		$sessionDataArray['searchField'] 	= $searchField;
		$sessionDataArray['searchText'] 	= $searchText;
		$sessionDataArray['searchDisplay'] 	= $searchDisplay;
	
		$this->nsession->set_userdata('ADMIN_EMAIL_TEMPLATE', $sessionDataArray);
		//==============================================================
	
		$orderBy = " order by ".$sortField." ".$sortType;
		//$where = " WHERE is_active ='Y' " ;
		$where = '';
	
		if($searchField <> "" && $searchText <> "" )
		{
			$where .= " AND ".$searchField. " like '%".$searchText."%' ";
		}
	
	
		$sql="SELECT COUNT(id) as TotalrecordCount FROM ".EMAIL_TEMPLATE_SETTING."  WHERE 1 $where $orderBy";
		$recordSet = $this->db->query($sql);
	
		$config['total_rows'] = 0;
		$config['per_page'] = $searchDisplay;
		if ($recordSet)
		{
			$row = $recordSet->row();
			$config['total_rows'] = $row->TotalrecordCount;
		}
		else
		{
			return false;
		}
	
		if($page > 0 && $page < $config['total_rows'] )
			$start = $page;
	
		$sql=" SELECT id,template_name,template_code,subject FROM ".EMAIL_TEMPLATE_SETTING." WHERE 1 $where $orderBy  LIMIT ".$start.",".$config['per_page'] ;
	
		$recordSet = $this->db->query($sql);
	
		$rs = false;
		if ($recordSet->num_rows() > 0)
		{
			$rs = array();
			$isEscapeArr = array();
			foreach ($recordSet->result_array() as $row)
			{
				foreach($row as $key=>$val){
					if(!in_array($key,$isEscapeArr)){
						$recordSet->fields[$key] = outputEscapeString($val);
					}
				}
				$rs[] = $recordSet->fields;
			}
		}
		else
		{
			return false;
		}
		return $rs;
	}
	
	/**
	 * Function getSingleEmailTemplate($id).
	 * used for get single template details.
	 * @param $id int template id
	 * @autor Santu Saha
	 * @date
	 */
	
	function getSingleEmailTemplate($id)
	{
		$sql = "SELECT * FROM ".EMAIL_TEMPLATE_SETTING." WHERE id = ".$id;
		$recordSet = $this->db->query($sql);
	
		$rs = false;
		if ($recordSet->num_rows() > 0)
		{
			$rs = array();
			$isEscapeArr = array('email_body');
			foreach ($recordSet->result_array() as $row)
			{
				foreach($row as $key=>$val){
					if(!in_array($key,$isEscapeArr)){
						$recordSet->fields[$key] = outputEscapeString($val);
					}else{
						$recordSet->fields[$key] = outputEscapeString($val,'TEXTAREA');
					}
				}
				$rs[] = $recordSet->fields;
			}
		}
		return $rs;
	}
	
	/**
	 * Function deleteEmailTemplate($id).
	 * used for delete template
	 * @param $id int template id
	 * @autor Santu Saha
	 * @date
	 */
	
	function deleteEmailTemplate($id)
	{
		$sql = "DELETE FROM ".EMAIL_TEMPLATE_SETTING." WHERE id = ".$id."";
		$recordSet = $this->db->query($sql);
	
		if (!$recordSet )
		{
			return false;
		}
	}
	
	/**
	 * Function  editEmailTemplate($id,$data).
	 * used for update template
	 * @param $id int template id
	 * @param $data array passing post value
	 * @autor Santu Saha
	 * @date
	 */
	
	function editEmailTemplate($id,$data)
	{
		$sql = "UPDATE ".EMAIL_TEMPLATE_SETTING."  SET template_name				= '".$data['template_name']."',
													   template_code	 			= '".$data['template_code']."',
													   from_name					= '".$data['from_name']."',
													   from_email					= '".$data['from_email']."',
													   subject						= '".$data['subject']."',
													   email_body		 			= '".$data['email_body']."'
													   WHERE id 					= ".$id."";
		
		$result = $this->db->query($sql);
		
		$affected_rows =$this->db->affected_rows();
		if($result){
			return $affected_rows;
		}else{
			return false;
		}
	}
	
	
	/**
	 * Function  addEmailTemplate($data).
	 * used for add new template
	 * @param $data array passing post value
	 * @autor Santu Saha
	 * @date
	 */
	function addEmailTemplate($data)
	{
		$sql = "INSERT INTO ".EMAIL_TEMPLATE_SETTING."  SET template_name				= '".$data['template_name']."',
															template_code	 			= '".$data['template_code']."',
															from_name					= '".$data['from_name']."',
															from_email					= '".$data['from_email']."',
															subject						= '".$data['subject']."',
															email_body		 			= '".$data['email_body']."'";
		
		$result = $this->db->query($sql);
		$last_insert_id = $this->db->insert_id();
	
		if($result){
			return $last_insert_id;
		}else{
			return false;
		}
	}
}

?>